package com.btobtob.a.biz.user.controller;

import com.alibaba.cola.dto.SingleResponse;
import io.dapr.client.DaprClient;
import io.dapr.client.domain.State;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@Tag(name = "stateManagement", description = "stateManagement")
@Validated
public class StateManagementController {

    @Resource
    private DaprClient client;

    private static final String STATE_STORE_NAME = "statestore";
    private static final String STATE_STORE_KEY = "one.more.society";

    /**
     * 保存状态
     *
     * @param value value
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public void save(String value) {
        log.info("save - value:{}", value);
        client.saveState(STATE_STORE_NAME, STATE_STORE_KEY, value).block();
    }

    /**
     * 读取状态
     *
     * @return StateResponse
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public SingleResponse<String> get() {
        State<String> value = client.getState(STATE_STORE_NAME, STATE_STORE_KEY, String.class).block();
        assert value != null;
        return SingleResponse.of(value.getValue());
    }

    /**
     * 删除状态
     *
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public void delete() {
        log.info("delete");
        client.deleteState(STATE_STORE_NAME, STATE_STORE_KEY).block();
    }
}
